package com.zsinfo.guoss.supplier.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.netcenter.Event.Response;
import com.example.netcenter.RetrofitBuilder;
import com.example.netcenter.Url.ConstantsCode;
import com.example.netcenter.bean.FruiteType;
import com.example.netcenter.bean.QuoteBean;
import com.example.netcenter.bean.ResultsData;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;
import com.zsinfo.guoss.supplier.R;
import com.zsinfo.guoss.supplier.base.BaseActivity;
import com.zsinfo.guoss.supplier.utils.SharedPreferencesUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lixl on 2018/12/6.
 * <p>
 * desc:报价记录
 * <p>
 * 报价状态：
 * 红色--竞价中
 * 绿色--竞价成功
 * 灰色--竞价失败
 */
public class FruitOfferListActivity extends BaseActivity {

    private XRecyclerView xRecyclerView;
    private List<QuoteBean.ObjectsBean> datas = new ArrayList<>();
    private CommonAdapter newsAdapter;
    private int CurrentNo = 1;

    @Override
    protected int getContentLayoutRes() {
        return R.layout.activity_fruit_list;
    }

    @Override
    protected void initView() {
        setTitle("报价记录");
        setBackAndLeftTitle("").setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        xRecyclerView = findViewById(R.id.recyclerview);
        xRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        xRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        xRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        xRecyclerView.setArrowImageView(R.mipmap.iconfont_downgrey);
        newsAdapter = new CommonAdapter<QuoteBean.ObjectsBean>(this, R.layout.item_fruit_offer, datas) {
            @Override
            protected void convert(final ViewHolder holder, final QuoteBean.ObjectsBean s, int position) {


                holder.setText(R.id.tv_time, s.getCreateTime()).setText(R.id.tv_name, "【" + s.getGoodsName() + "】");

                if (s.getStatus() == 1) {
                    holder.setText(R.id.tv_state, "竞价成功").setTextColor(R.id.tv_state, Color.GREEN);
                } else if (s.getStatus() == -1) {
                    holder.setText(R.id.tv_state, "竞价失败").setTextColor(R.id.tv_state, Color.GRAY);
                } else {
                    holder.setText(R.id.tv_state, "竞价中").setTextColor(R.id.tv_state, Color.RED);
                }

                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //查看详情
                        Intent i = new Intent(FruitOfferListActivity.this, FruitOfferDetailActivity.class);
                        i.putExtra("quotedId", s.getId());
                        startActivity(i);
                    }
                });

            }
        };
        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                //刷新操作
                CurrentNo = 1;
                initNetData();
            }

            @Override
            public void onLoadMore() {
                //加载更多
                CurrentNo++;
                initNetData();
            }
        });

        xRecyclerView.setAdapter(newsAdapter);

        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                CurrentNo = 1;
                initNetData();
            }

            @Override
            public void onLoadMore() {
                CurrentNo++;
                initNetData();
            }
        });
    }

    @Override
    protected void initNetData() {

        GetNetCenter().GetNetCenter(RetrofitBuilder.CreateApi().QUOTED_LIST(ConstantsCode.quoted_list,
                SharedPreferencesUtil.getID(),
                CurrentNo + "",
                "15"),
                new Response() {
                    @Override
                    public void OK(Object data) {

                        ResultsData<QuoteBean> resultsData = (ResultsData<QuoteBean>) data;

                        if (CurrentNo == 1) {
                            datas.clear();
                            if (resultsData.getData().getObjects().size() > 0) {
                                SetNoData(false);
                            } else {
                                SetNoData(true);
                            }
                        }

                        datas.addAll(resultsData.getData().getObjects());

                        newsAdapter.notifyDataSetChanged();


                    }

                    @Override
                    public void Fail(Throwable e) {

                    }

                    @Override
                    public void complete() {
                        xRecyclerView.refreshComplete();
                        xRecyclerView.loadMoreComplete();

                    }
                });


    }

    @Override
    protected void customDestroy() {

    }


}



